package com.book.service.impl;

import com.book.activeMQ.productor.ProductMQ;
import com.book.dao.BookDao;
import com.book.dto.BookDto;
import com.book.helper.UBookHelper;
import com.book.pojo.Book;
import com.book.service.BookService;
import com.book.util.Constants;
import com.book.util.RedisUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author LiJinCheng
 * @Date 2020/11/16 8:52 下午
 * @Version 1.0
 * @package com.book.service.impl
 **/
@Slf4j
@Service
public class BookServiceImpl implements BookService {

    @Resource
    private BookDao uBookDao;
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private ProductMQ productMQ;


    @Override
    public Integer addUBook(BookDto uBook) {
        try {
            uBook.setBootTime(new Date());
            uBook.setBookFlag((short) 1);
            Book uBook1 = UBookHelper.covertDtpPO(uBook);
            int i = uBookDao.insertSelective(uBook1);
            if (i == 1) {
                Book uBookByCode = uBookDao.getUBookByCode(uBook.getBookCode());
                redisUtil.rPush("u_book", uBookByCode);
            }
            return i;
        } catch (Exception e) {
            log.error("BookServiceImpl.addUBook#异常", e);
            return 0;
        }
    }


    @Override
    public List<BookDto> getUBookList() {
        log.info("#UBookServiceImpl.getUBookList#");
        boolean exist = redisUtil.exists(Constants.USER_BOOK);
        if (exist) {
            return (List<BookDto>) redisUtil.lRange(Constants.USER_BOOK, 0, -1, BookDto.class);
        } else {
            List<Book> listPo = uBookDao.getUBookList();
            if (CollectionUtils.isEmpty(listPo)) {
                return Lists.newArrayList();
            }
            List<BookDto> uBookDtos = UBookHelper.covertList(listPo);
            for (BookDto u :
                    uBookDtos) {
                redisUtil.rPush(Constants.USER_BOOK, u);
            }
            return uBookDtos;
        }
    }

    @Override
    public BookDto getUBookByCode(String ubCode) {
        Book uBook = uBookDao.getUBookByCode(ubCode);
        return UBookHelper.covertPoDto(uBook);
    }

    @Override
    public Integer insertSelective(Object o) {
        return null;
    }

    @Override
    public Integer updateSelective(Object o) {
        if (o == null) {
            return 0;
        }
        BookDto uBookDto = (BookDto) o;
        log.info("UBookServiceImpl.updateSelective#uBookDto:{}", uBookDto);
        return uBookDao.updateByPrimaryKeySelective(UBookHelper.covertDtpPO(uBookDto));
    }

    @Override
    public BookDto selectById(Object o) {
        Integer id = (Integer) o;
        Book uBook = uBookDao.selectByPrimaryKey(id);
        return UBookHelper.covertPoDto(uBook);
    }

    @Override
    public String getUBCode(Integer parseInt) {
        return uBookDao.getUBCode(parseInt);
    }

    @Override
    public List<BookDto> getBookThree() {
        boolean three_book = redisUtil.exists(Constants.NEW_BOOK);
        if (three_book) {
            List<BookDto> three_book1 = (List<BookDto>) redisUtil.lRange(Constants.NEW_BOOK, 0, 3, BookDto.class);
            return three_book1;
        }
        List<Book> list = uBookDao.getBookThree();
        log.info("BookServiceImpl#list:{}", list);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        List<BookDto> list1 = list.stream().map(UBookHelper::covertPoDto).collect(Collectors.toList());
        productMQ.updateRedisNewBook(list1);
        return list1;
    }

    @Override
    public List<BookDto> getBookListByUId(Integer currentId) {
        List<Book> list = uBookDao.getBookListByUId(currentId);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        return list.stream().map(UBookHelper::covertPoDto).collect(Collectors.toList());
    }

    @Override
    public List<BookDto> queryBookByAuthorOrTitle(Integer queryCode, String queryText) {
        List<Book> books = uBookDao.queryBookByKey(queryCode, queryText);
        if (CollectionUtils.isEmpty(books)) {
            return Lists.newArrayList();
        }
        return books.stream().map(UBookHelper::covertPoDto).collect(Collectors.toList());
    }

    @Override
    public List<BookDto> queryBook(String keyWord) {
        String[] s = keyWord.split(" ");
        String key1 = s[0];
        String key2 = "";
        if (s.length == 1) {
            key2 = s[0];
        } else {
            key2 = s[1];
        }
        List<Book> list = uBookDao.queryBook(key1, key2);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        return list.stream().map(UBookHelper::covertPoDto).collect(Collectors.toList());
    }

    @Override
    public List<BookDto> queryBook(List<Integer> cateIdList, String queryText) {
        List<Book> list = uBookDao.queryBookByCate(cateIdList, queryText);
        return list.stream().map(UBookHelper::covertPoDto).collect(Collectors.toList());
    }

    @Override
    public List<BookDto> searchFilter(List<Integer> cateIdList, String price) {
        String[] s = price.split(",");
        Double p1 = Double.valueOf(s[0]);
        Double p2 = Double.valueOf(s[1]);
        List<Book> list = uBookDao.queryFilterCateList(cateIdList, p1, p2);
        if (CollectionUtils.isEmpty(list)){
            return Lists.newArrayList();
        }
        return UBookHelper.covertList(list);
    }

    @Override
    public List<BookDto> searchFilter(Integer cateId, String price) {
        String[] s = price.split(",");
        Double p1 = Double.valueOf(s[0]);
        Double p2 = Double.valueOf(s[1]);
        log.info("BookServiceImpl.searchFilter#cateId:{}",cateId);
        List<Book> list = uBookDao.queryBookByCatePrice(cateId, p1, p2);
        if (CollectionUtils.isEmpty(list)){
            return Lists.newArrayList();
        }
        return UBookHelper.covertList(list);
    }

    @Override
    public List<BookDto> getBookListByBookCode(List<String> codeList) {
        List<Book> bookListByBookCode = uBookDao.getBookListByBookCode(codeList);
        if (CollectionUtils.isEmpty(bookListByBookCode)){
            return Lists.newArrayList();
        }
        return bookListByBookCode.stream().map(UBookHelper::covertPoDto).collect(Collectors.toList());
    }

    @Override
    public Integer deleteBookById(Integer id) {
        return uBookDao.deleteBookById(id);
    }

    @Override
    public void batchDeleteBok(List<Integer> ids) {
        uBookDao.deleteBookByIdList(ids);
    }
}
